/*
 * Created by JFormDesigner on Mon Mar 28 12:57:15 CST 2022
 */

package com.gxu.sjjg;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * @author unknown
 */
public class DeleteGrade extends JFrame {//删除成绩界面即相关代码
    public DeleteGrade() {
        initComponents();
    }

    //删除学生数据代码
    private void button1(ActionEvent e) {//button1是删除按钮
        if(textField1.getText().equals("")||textField1.getText().length()>10)//如果输入为空，或字符长度大于10，提示数据不合法。
            JOptionPane.showMessageDialog(null, "数据不合法！", "提示", JOptionPane.WARNING_MESSAGE);
        else {
            try {
                String[] imf=new String[100];
                int j=0;
                BufferedReader fbr = new BufferedReader(new FileReader("src/file/Grade.txt"));
                imf[j++]=fbr.readLine();//先把第一行读完
                String s;
                boolean exsist = false;
                while ((s = fbr.readLine()) != null) {//把除了待删除数据以外的所有数据按行存入数组
                    int i = 0;
                    if (s.substring(i + 10, i + 19).trim().equals(textField1.getText())) {//如果不是该学生，就添加到数组，是的话就不添加
                        exsist=true;//删除成功，且说明输入的学号存在
                    }
                    else
                        imf[j++] = "" + s;
                }
                fbr.close();
                if (exsist) {//如果输入的学号存在，就将数组里的数据以覆盖的方式写回文件，这样就实现了数据的删除
                    BufferedWriter inbf = new BufferedWriter(new FileWriter("src/file/Grade.txt"));
                    for (int i = 0; i < j; i++){//将数组里的数据以覆盖的方式写回文件
                        inbf.write(imf[i]);
                        inbf.newLine();
                    }
                    inbf.close();
                    JOptionPane.showMessageDialog(null, "删除成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                } else
                    JOptionPane.showMessageDialog(null, "不存在该生信息！", "提示", JOptionPane.WARNING_MESSAGE);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
    }

    //界面代码
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        label2 = new JLabel();

        //======== this ========
        setResizable(false);
        setVisible(true);
        setTitle("\u5220\u9664");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8bf7\u8f93\u5165\u5b66\u751f\u5b66\u53f7");
        contentPane.add(label1);
        label1.setBounds(60, 30, 90, 20);
        contentPane.add(textField1);
        textField1.setBounds(55, 70, 105, textField1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u5220\u9664");
        button1.addActionListener(e -> button1(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(75, 120), button1.getPreferredSize()));

        //---- label2 ----
        label2.setIcon(new ImageIcon(getClass().getResource("/myimage/loginimg.jpg")));
        contentPane.add(label2);
        label2.setBounds(0, 0, 220, 180);

        contentPane.setPreferredSize(new Dimension(220, 210));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JLabel label2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
